/*--------------------------------------------------------------------------------------+
|
|     $Source: samples/SampleDgnDbEditor/DeleteShippingItemTool.cpp $
|
|  $Copyright: (c) 2014 Bentley Systems, Incorporated. All rights reserved. $
|
+--------------------------------------------------------------------------------------*/
#include "DeleteShippingItemTool.h"

//---------------------------------------------------------------------------------------
// @bsimethod
//---------------------------------------------------------------------------------------
bool DeleteShippingItemTool::IsDeleteValid (ECClassId ecClassId)
    {
    return Shipping%Class%ItemHandler::Get().CanHandleClass (m_writeModel.GetDgnProject(), ecClassId)
        || ShippingCrateItemHandler::Get().CanHandleClass (m_writeModel.GetDgnProject(), ecClassId);
    }

//---------------------------------------------------------------------------------------
// @bsimethod
//---------------------------------------------------------------------------------------
bool DeleteShippingItemTool::_OnDataButton (DgnButtonEventCP ev)
    {
    // second data button confirms the delete
    if (IsDeleteInProgress())
        {
        // delete physical item graphics
        EditElementHandle eeh (m_deleteElementId, m_writeModel);
        eeh.DeleteFromModel();

        // delete physical item instance
        ECClassP classP = NULL;
        if (BSISUCCESS == m_writeModel.GetDgnProject().GetEC().GetSchemaManager().GetECClass (classP, m_deleteItemKey.GetECClassId()))
            {
            ECInstanceDeleter instanceDeleter (m_writeModel.GetDgnProject(), *classP);
            instanceDeleter.Delete (m_deleteItemKey.GetECInstanceId());
            }

        _CloseCurrentTxn();
        _ClearSelection();
        _OnRestartTool();
        return false;
        }

    // a delete is not currently in progress so try to identify an element to delete
    HitPathCP hitPathCP = ElementLocateManager::GetManager().DoLocateHitPath (NULL, NULL, true, ev->GetPoint(), ev->GetViewport(), 0);
    if (hitPathCP && hitPathCP->GetHeadElem())
        {
        EditElementHandle eeh (hitPathCP->GetHeadElem());
        ECInstanceKey itemKey;

        if (DgnECPersistence::GetPrimaryInstanceOnElement (itemKey, eeh) && IsDeleteValid (itemKey.GetECClassId()))
            {
            m_deleteElementId = hitPathCP->GetHeadElem()->GetElementId();
            m_deleteItemKey = itemKey;
            return false;
            }
        }

    // no elements to delete were found, reset state for next _OnDataButton
    m_deleteElementId.Invalidate();
    m_deleteItemKey = ECInstanceKey();
    _ClearSelection();
    return false;
    }

//---------------------------------------------------------------------------------------
// @bsimethod
//---------------------------------------------------------------------------------------
bool DeleteShippingItemTool::_OnResetButton (DgnButtonEventCP ev)
    {
    _OnRestartTool();
    return false;
    }

//---------------------------------------------------------------------------------------
// @bsimethod
//---------------------------------------------------------------------------------------
void DeleteShippingItemTool::_OnRestartTool() 
    {
    if (_CheckSingleShot())
        return;

    DeleteShippingItemTool* newTool = new DeleteShippingItemTool (m_writeModel);
    newTool->InstallTool();
    }

//---------------------------------------------------------------------------------------
// @bsimethod
//---------------------------------------------------------------------------------------
void DeleteShippingItemTool::_OnPostInstall()
    {
    T_Super::_OnPostInstall();

    AccuDraw::GetInstance().Deactivate();
    AccuSnap::GetInstance().EnableLocate (true);
    AccuSnap::GetInstance().EnableSnap (false);
    }

//---------------------------------------------------------------------------------------
// @bsimethod
//---------------------------------------------------------------------------------------
DeleteShippingItemTool::DeleteShippingItemTool (DgnModelR writeModel) 
    : DgnPrimitiveTool (0), m_writeModel (writeModel)
    {
    }
